#include "PathSettings.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QFileDialog>
#include <QCoreApplication>

PathSettings::PathSettings(QWidget *parent)
    : QDialog(parent),
      settings(QCoreApplication::applicationDirPath() + "/path.ini", QSettings::IniFormat)
{
    setWindowTitle("修改文件路径");

    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // 已知人脸文件夹输入控件
    QHBoxLayout *knownFacesDirLayout = new QHBoxLayout();
    knownFacesDirInput = new QLineEdit(this);
    knownFacesDirInput->setPlaceholderText("默认: " + QCoreApplication::applicationDirPath() + "/face");
    knownFacesDirButton = new QPushButton("选择", this);
    knownFacesDirLayout->addWidget(new QLabel("已知人脸文件夹:", this));
    knownFacesDirLayout->addWidget(knownFacesDirInput);
    knownFacesDirLayout->addWidget(knownFacesDirButton);
    mainLayout->addLayout(knownFacesDirLayout);

    // 遍历文件夹输入控件
    QHBoxLayout *scanFolderLayout = new QHBoxLayout();
    scanFolderInput = new QLineEdit(this);
    scanFolderInput->setPlaceholderText("默认: " + QCoreApplication::applicationDirPath() + "/fileDir");
    scanFolderButton = new QPushButton("选择", this);
    scanFolderLayout->addWidget(new QLabel("遍历文件夹:", this));
    scanFolderLayout->addWidget(scanFolderInput);
    scanFolderLayout->addWidget(scanFolderButton);
    mainLayout->addLayout(scanFolderLayout);

    // 保存按钮
    saveButton = new QPushButton("保存", this);
    mainLayout->addWidget(saveButton);

    connect(knownFacesDirButton, &QPushButton::clicked, this, &PathSettings::selectKnownFacesDir);
    connect(scanFolderButton, &QPushButton::clicked, this, &PathSettings::selectScanFolder);
    connect(saveButton, &QPushButton::clicked, this, &PathSettings::saveSettings);

    loadSettings();
}

void PathSettings::selectKnownFacesDir()
{
    QString dir = QFileDialog::getExistingDirectory(this, "选择已知人脸文件夹", QDir::homePath());
    if (!dir.isEmpty()) {
        knownFacesDirInput->setText(dir);
    }
}

void PathSettings::selectScanFolder()
{
    QString dir = QFileDialog::getExistingDirectory(this, "选择遍历文件夹", QDir::homePath());
    if (!dir.isEmpty()) {
        scanFolderInput->setText(dir);
    }
}

void PathSettings::saveSettings()
{
    settings.setValue("KnownFacesDir", knownFacesDirInput->text());
    settings.setValue("ScanFolder", scanFolderInput->text());
    accept();
}

void PathSettings::loadSettings()
{
    knownFacesDirInput->setText(settings.value("KnownFacesDir", QCoreApplication::applicationDirPath() + "/face").toString());
    scanFolderInput->setText(settings.value("ScanFolder", QCoreApplication::applicationDirPath() + "/fileDir").toString());
}

QString PathSettings::getKnownFacesDir() const
{
    return knownFacesDirInput->text().isEmpty() ? QCoreApplication::applicationDirPath() + "/face" : knownFacesDirInput->text();
}

QString PathSettings::getScanFolder() const
{
    return scanFolderInput->text().isEmpty() ? QCoreApplication::applicationDirPath() + "/fileDir" : scanFolderInput->text();
}

void PathSettings::setKnownFacesDir(const QString &dir)
{
    knownFacesDirInput->setText(dir);
}

void PathSettings::setScanFolder(const QString &folder)
{
    scanFolderInput->setText(folder);
}
